package org.example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.IntFunction;

public class Stream_Demo3 {
    public static void main(String[] args) {
        //map 映射
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list, "张三-13", "李四-14", "王五-15", "张六财-16", "张七望-17", "孙八-18");
        list.stream().map(new Function<String, Integer>() {
            @Override
            public Integer apply(String s) {
                String age = s.split("-")[1];
                int ageInt = Integer.parseInt(age);
                return ageInt;
            }
        }).forEach(s -> System.out.println(s));
        System.out.println("------------------1");
        list.stream().map(s->s.split("-")[1]).forEach(s -> System.out.println(s));
        //forEach 遍历
        ArrayList<String> list1 = new ArrayList<>();
        Collections.addAll(list1, "张三", "李四", "王五", "张六财", "张七望", "孙八");
        list1.stream().forEach(s -> System.out.println(s));
        System.out.println("------------------2");
        list1.stream().forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });
        System.out.println("------------------3");
        //count 统计
        long count = list1.stream().count();
        System.out.println(count);
        //toArray 收集流中的元素 放到数组中
        System.out.println("-----------------4");
        Object[] arr1 = list1.stream().toArray();
        System.out.println(Arrays.toString(arr1));
        System.out.println("-----------------5");
        //方法体创建数组
        //toArray 方法体创建数组
        //底层调用了方法体的到流中每个数据创建数组并存入
        String[] arr2 = list1.stream().toArray(new IntFunction<String[]>() {
            @Override
            public String[] apply(int value) {
                //value 流中数据个数 要与数组长度一样
                //返回值是具体类型数组
                return new String[value];
            }
        });
        System.out.println(Arrays.toString(arr2));
        System.out.println("------------------6");
        String[]arr3 = list1.stream().toArray(value->new String[value]);
        System.out.println(Arrays.toString(arr3));

    }
}
